<?php

namespace app\api\controller\ai;

use app\common\controller\UserBase;
use app\common\model\QwenOrder;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;

class Qwen extends UserBase
{
    public function create()
    {
        $data = input();
        $data['user_id'] = $this->uid;
        if ($this->user->qwen_token < 1000) {
            $this->error("token不足,请充值");
        }
        $rule = [
            "prompt|描述" => "require",
        ];
        Db::startTrans();
        try {
            $this->validate($data, $rule);
            $exit = QwenOrder::create([
                "user_id" => $this->uid,
                "prompt" => $data['prompt'],
            ]);
            Db::commit();
            $config = cache('config');
            queue('app\common\job\AiOrder@qwen',[
                "id" => $exit->id,'prompt' => $data['prompt'],'model' => $config['qwen_model']
            ],0,'qwen');
            $this->success($exit);
        } catch (ValidateException | Exception $exception) {
            Db::rollback();
            $this->error($exception->getMessage());
        }
    }

    public function getDetail()
    {
        $id = input('id');
        $exit = QwenOrder::where('id',$id)->where('user_id',$this->uid)->find();
        if (!$exit) {
            $this->error("数据不存在");
        }
        $this->success($exit);
    }
}